<?php

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-10-19
 * Time: 15:00
 */
namespace App\Http\Biz;

use App\Http\Model\ConsoleAccount;
use App\Http\Model\ConsoleAccountRole;
use App\Http\Model\ConsoleApps;
use App\Http\Model\ConsoleRole;
use App\Http\Model\ConsoleRoleApps;
use App\Http\Model\PtGoods;
use App\Http\Model\PtGoodsOrderMid;
use App\Http\Model\PtGoodsSpec;
use App\Http\Model\PtMaterial;
use App\Http\Model\PtOrder;
use App\Http\Model\PtPreSale;
use App\Http\Model\Shop;
use App\Http\Model\ShopAccount;
use App\Http\Model\ShopAccountRoleMid;
use App\Http\Model\ShopAddGoods;
use App\Http\Model\ShopAddGoodsMid;
use App\Http\Model\ShopGoods;
use App\Http\Model\ShopGoodsInoutMid;
use App\Http\Model\ShopGoodsOrderMid;
use App\Http\Model\ShopModule;
use App\Http\Model\ShopOrder;
use App\Http\Model\ShopRole;
use App\Http\Model\ShopRoleModuleMid;
use App\Http\Model\ShopSupp;
use App\Http\Model\ShopSuppProtocolMid;
use App\Http\Model\ShopTorage;
use App\Http\Model\ShopTorageInOut;
use App\Http\Model\Supp;
use App\Http\Model\SuppAccountRoleMid;
use App\Http\Model\SuppModule;
use App\Http\Model\SuppPurchase;
use App\Http\Model\SuppPurchaseMid;
use App\Http\Model\SuppRoleModuleMid;
use App\Http\Utils\Common\Consts;
use App\Http\Utils\Common\RoleUtils;
use Carbon\Carbon;
use Exception;
use GuzzleHttp\Client;
use Illuminate\Support\Facades\DB;
use PDOException;
use Log;

class SuppOrderBiz
{
    public function mainDataBiz($supp_id, $account_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            //查出该账号的角色
            $role = ConsoleAccountRole::where(['account_id' => $account_id,'ownto_id'=>$supp_id,'account_types'=>1])->first();
            //查出该角色的模块列表
            $apps_ids = ConsoleRoleApps::where('role_id', $role->role_id)->pluck('apps_id')->all();
            $module = ConsoleApps::whereIn('id', $apps_ids)->where(['types' => 1, 'env_types' => 1, 'is_online' => '0','module_type'=>'0'])
                ->orderBy('seq', 'asc')->get(['name', 'base_url as url', 'icon']);

            $module2 = ConsoleApps::whereIn('id', $apps_ids)->where(['types' => 1, 'env_types' => 1, 'is_online' => '0','module_type'=>'1'])
                ->orderBy('seq', 'asc')->get(['name as title', 'base_url as url', 'icon']);
            $result['module'] = $module;
            if($module2!=null&&count($module2)>0){
                foreach ($module2 as $m){
                    $m->mode='multiSelector';
                    $m->oper_name='bindMultiPickerChange';
                }
            }
            $result['module2'] = $module2;

            $supp = Supp::where('id',$supp_id)->first();
            $supp->phone = ConsoleAccount::where('id',$account_id)->first()->account;
            $result['supp'] = $supp;
            $result['role'] = ConsoleRole::find($role->role_id);
            $now = Carbon::now();
            $data = PtGoodsOrderMid::where('supp_id', $supp_id)->where('status', '>=', 2)->where(['year' => $now->year, 'month' => $now->month, 'day' => $now->day])->groupBy('supp_id')->get([DB::raw('sum(price) as price'), DB::raw('count(order_num) as num')]);
//            Log::info($data);
            $price = 0;
            $num = 0;
            foreach ($data as $item) {
                $price = $item->price;
                $num = $item->num;
            }
            $result['today_price'] = $price;
            $result['today_order_nums'] = $num;
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $result;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return [];
        }
    }

    //销售发货的提货点列表
    public function readPickupByDayBiz($supp_id, $year, $month, $day,$serial_num,$account_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            //查询供应商售卖截止点
//            $supp = Supp::find($supp_id);
//            $sale_end = $year . '-' . $month . '-' . $day . ' ' . $supp->sale_end . ':00';
//            $time = Carbon::parse($sale_end);
            $query = PtGoodsOrderMid::query();
            $role_ids = ConsoleAccountRole::where(['account_id'=>$account_id,'ownto_id'=>$supp_id,'account_types'=>1])->pluck('role_id')->all();
            if(count($role_ids) == 0){
                return [];
            }
            $role_ids = array_intersect($role_ids,Consts::admin_role_supp_sub);//去数组的交集
            //数据权限
            if(count($role_ids) > 0){
                $goods_ids = RoleUtils::getRightData($account_id,$supp_id,1);
                $query = $query->whereIn('goods_id',$goods_ids);
            }

            //查询售卖截止前的所有订单
            $data = $query
                ->where(['supp_id' => $supp_id])
                ->where('status', '>=', 2)
                ->where('serial_num',$serial_num)
//                ->where(['supp_year' => $year, 'supp_month' => $month, 'supp_day' => $day])
                ->orderBy('pickup_id','asc')
                ->orderBy('goods_name','asc')
                ->orderBy('seq_in_serial','asc')
                ->get(['order_num', 'num', 'pickup_id', 'spec_name','spec_id', 'goods_name','seq_in_serial']);
//            return $data;
            //以提货点为基准来进行分组
            foreach ($data as $v) {
                $shop = Shop::find($v->pickup_id);
//                unset($v->pickup_id);
                if ($shop != null) {
                    $addr = $shop->pro . $shop->city . $shop->area . $shop->addr;
                } else {
                    $addr = '未知，请联系管理员';
                }
                $v->addr = $addr;
                $user = PtOrder::where('order_num', $v->order_num)->first();
                if ($user != null) {
                    $v->user = $user->user;
                    $v->phone = $user->phone;
                }
            }
            $result = $data->groupBy(['addr']);
            $arr = [];
            foreach ($result as $addr => $item) {
                $tmp['location'] = $addr;
                $specGroup = $item->groupBy('spec_id');
                $dataArr = [];
                $tmpPickup_id = '';
                foreach ($specGroup as $k => $v) {
                    $num = 0;
                    $v_arr = $v->toArray();
                    $pu['spec_name'] = $v_arr[0]['spec_name'];
                    $pu['goods_name'] = $v_arr[0]['goods_name'];
//                    $pu['user'] = $v_arr[0]['user'];
                    foreach ($v_arr as $i){
                        $num += $i['num'];
                    }
                    $pu['total_num'] = $num;
                    $pu['goods'] = $v;
                    $tmpPickup_id = $v_arr[0]['pickup_id'];
                    array_push($dataArr, $pu);
                }
                $tmp['pickup_id'] = $tmpPickup_id;
//                $tmp['total_num'] = $num;
                $tmp['data'] = $dataArr;
                $inout = ShopTorageInOut::where(['serial_num'=>$serial_num, 'pickup_id' => $tmpPickup_id, 'supp_id' => $supp_id])->first();
                if ($inout != null) {
                    if ($inout->status == 0 || $inout->status == 1) {
                        $tmp['is_out'] = 1;//已发货了
                    } else {
                        $tmp['is_out'] = 0;//确认发货按钮
                    }
                } else {
                    $tmp['is_out'] = 0;
                }
                array_push($arr, $tmp);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $arr;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return [];
        }
    }

    //销售发货的提货点列表（备份）
    public function readPickupByDayBiz2($supp_id, $year, $month, $day)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            //查询供应商售卖截止点
            $supp = Supp::find($supp_id);
            $sale_end = $year . '-' . $month . '-' . $day . ' ' . $supp->sale_end . ':00';
            $time = Carbon::parse($sale_end);
            //查询售卖截止前的所有订单
            $data = PtGoodsOrderMid::query()
                ->where(['supp_id' => $supp_id])
                ->where('status', '>=', 2)
                ->where(['supp_year' => $year, 'supp_month' => $month, 'supp_day' => $day])
                ->get(['order_num', 'num', 'pickup_id', 'spec_name', 'goods_name']);
//            return $data;
            //以提货点为基准来进行分组
            foreach ($data as $v) {
                $shop = Shop::find($v->pickup_id);
//                unset($v->pickup_id);
                if ($shop != null) {
                    $addr = $shop->pro . $shop->city . $shop->area . $shop->addr;
                } else {
                    $addr = '未知，请联系管理员';
                }
                $v->addr = $addr;
                $user = PtOrder::where('order_num', $v->order_num)->first();
                if ($user != null) {
                    $v->user = $user->user;
                    $v->phone = $user->phone;
                }
            }
            $result = $data->groupBy(['addr']);
            $arr = [];
            foreach ($result as $addr => $item) {
                $tmp['location'] = $addr;
                $orderGroup = $item->groupBy('order_num');
                $dataArr = [];
                $tmpPickup_id = '';
                foreach ($orderGroup as $k => $v) {
                    $pu['order_num'] = $k;
                    $pu['goods'] = $v;
                    $tmpPickup_id = $v->toArray()[0]['pickup_id'];
                    array_push($dataArr, $pu);
                }
                $tmp['pickup_id'] = $tmpPickup_id;
                $tmp['data'] = $dataArr;
                $inout = ShopTorageInOut::where(['year' => $year, 'month' => $month, 'day' => $day, 'pickup_id' => $tmpPickup_id, 'supp_id' => $supp_id])->first();
                if ($inout != null) {
                    if ($inout->status == 0 || $inout->status == 1) {
                        $tmp['is_out'] = 1;//已发货了
                    } else {
                        $tmp['is_out'] = 0;//确认发货按钮
                    }
                } else {
                    $tmp['is_out'] = 0;
                }
                array_push($arr, $tmp);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $arr;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return [];
        }
    }

    //根据日期查询详细的采购列表数据
    public function readSaleListByDayBiz($supp_id, $purchase_id,$account_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $role_ids = ConsoleAccountRole::where(['account_id'=>$account_id,'ownto_id'=>$supp_id,'account_types'=>1])->pluck('role_id')->all();
            if(count($role_ids) == 0){
                return [];
            }
            $role_ids = array_intersect($role_ids,Consts::admin_role_supp_sub);//去数组的交集
            Log::info($account_id);
            Log::info($supp_id);
            $query = SuppPurchaseMid::where('purchase_id', $purchase_id);
            //数据权限
            if(count($role_ids)>0){
                $goods_ids = RoleUtils::getRightData($account_id,$supp_id,1);
                Log::info($goods_ids);
                $query = $query->whereIn('goods_id',$goods_ids);
            }
            $data = $query->get();
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $data;
//            $supp = Supp::find($supp_id);
//            $now = Carbon::now();
//            if($now->year==$year && $now->month==$month && $now->day==$day){
//                $status = [2];
//            }else{
//                $status = [2,3,4,5];
//            }
//            $sale_end = $year . '-' . $month . '-' . $day . ' ' . $supp->sale_end . ':00';
//            $time = Carbon::parse($sale_end);
//            $data = PtGoodsOrderMid::query()->where('supp_id', $supp_id)
//                ->whereIn('status', $status)
////                ->where(['year' => $year, 'month' => $month, 'day' => $day])
//                ->where('updated_at', '<=', $time)
//                ->groupBy('spec_id')
//                ->get([DB::raw('sum(num) as num'), 'spec_name', 'spec_id', 'goods_name']);
//            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
//            foreach ($data as $item) {
//                $spec = PtGoodsSpec::find($item->spec_id);
//                $item->spec_unit = $spec->sale_unit_name;
//                $item->sale_unit_val = $item->num * $spec->sale_unit_val;
//                unset($item->num);
//            }
//            return $data;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return [];
        }
    }

    //按采购表查询所有日期列表接口（分页）
    public function readSaleDataByPageBiz($supp_id,$account_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $role_ids = ConsoleAccountRole::where(['account_id'=>$account_id,'ownto_id'=>$supp_id,'account_types'=>1])->pluck('role_id')->all();
            if(count($role_ids) == 0){
                return [];
            }
            $role_ids = array_intersect($role_ids,Consts::admin_role_supp_sub);//去数组的交集
            $query = SuppPurchase::query();
            //数据权限
            if(count($role_ids) > 0){
                $goods_ids = RoleUtils::getRightData($account_id,$supp_id,1);
                $mid = PtGoodsOrderMid::whereIn('goods_id',$goods_ids)->get();
                if(count($mid)>0){
                    $serial_nums = $mid->unique('serial_num')->pluck('serial_num')->all();
                    $query = $query->whereIn('serial_num',$serial_nums);
                }else{
                    $query = $query->whereIn('serial_num',[]);
                }
            }
            $data = $query
                ->where('supp_purchase.supp_id', $supp_id)
                ->orderBy('supp_purchase.year', 'desc')
                ->orderBy('supp_purchase.month', 'desc')
                ->orderBy('supp_purchase.day', 'desc')
                ->paginate(Consts::PAGE_SIZE);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            if (count($data->items()) == 0) {
                return null;
            }
            return $data;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return null;
        }
    }

    //按天查询商品总订单数和总金额列表接口
    public function readOrderByDateBiz($supp_id, $year, $month, $day,$account_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $query = PtGoodsOrderMid::query();
            $role_ids = ConsoleAccountRole::where(['account_id'=>$account_id,'ownto_id'=>$supp_id,'account_types'=>1])->pluck('role_id')->all();
            if(count($role_ids) == 0){
                return [];
            }
            $role_ids = array_intersect($role_ids,Consts::admin_role_supp_sub);//去数组的交集
            //数据权限
            if(count($role_ids) > 0){
                $goods_ids = RoleUtils::getRightData($account_id,$supp_id,1);
                $query = $query->whereIn('goods_id',$goods_ids);
            }
            $data = $query->where('supp_id', $supp_id)
                ->whereIn('status', Consts::order_pay_status)
                ->where(['year' => $year, 'month' => $month, 'day' => $day])
                ->groupBy('goods_id')
                ->get([DB::raw('count(order_num) as num'), DB::raw('sum(price) as price'), 'goods_mirror_data']);
            foreach ($data as $item) {
                $item->goods_name = json_decode($item->goods_mirror_data)->goods_name;
                unset($item->goods_mirror_data);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $data;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return [];
        }
    }

    //按天查询统计列表（分页）接口，展示形式：日期 订单总数 订单总额
    public function readOrderDateByPageBiz($supp_id,$account_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $query = PtGoodsOrderMid::query()
                ->select(DB::raw('count(order_num) as num'), DB::raw('sum(price) as price'), 'year', 'month', 'day')
                ->where('supp_id', $supp_id)
                ->whereIn('status', Consts::order_pay_status);
            $role_ids = ConsoleAccountRole::where(['account_id'=>$account_id,'ownto_id'=>$supp_id,'account_types'=>1])->pluck('role_id')->all();
            if(count($role_ids) == 0){
                return [];
            }
            $role_ids = array_intersect($role_ids,Consts::admin_role_supp_sub);//去数组的交集

            //数据权限
            if(count($role_ids) > 0){
                $goods_ids = RoleUtils::getRightData($account_id,$supp_id,1);
                $query = $query->whereIn('goods_id',$goods_ids);
            }
            $data = $query
                ->groupBy('year')
                ->groupBy('month')
                ->groupBy('day')
                ->orderBy('created_at', 'desc')
                ->paginate(Consts::PAGE_SIZE);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            if (count($data->items()) == 0) {
                return null;
            }
            return $data;


            $data = PtGoodsOrderMid::query()->where('supp_id', $supp_id)->where('status', 2)->get(['order_num', 'num', 'price', 'pickup_id', 'goods_id', 'spec_name', 'goods_mirror_data', 'spec_id']);
            foreach ($data as $v) {
                $shop = Shop::find($v->pickup_id);
                if ($shop != null) {
                    $addr = $shop->pro . $shop->city . $shop->area . $shop->addr;
                } else {
                    $addr = '未知，请联系管理员';
                }
                $v->addr = $addr;
                $v->goods_name = json_decode($v->goods_mirror_data)->goods_name;
                $user = PtOrder::where('order_num', $v->order_num)->first();
                if ($user != null) {
                    $v->user = $user->user;
                    $v->phone = $user->phone;
                }
                unset($v->goods_mirror_data);
            }
            $result = $data->groupBy(['addr']);
            $arr = [];
            foreach ($result as $addr => $item) {
                $tmp['location'] = $addr;
                $orderGroup = $item->groupBy('order_num');
                $dataArr = [];
                foreach ($orderGroup as $k => $v) {
                    $pu['order_num'] = $k;
                    $pu['goods'] = $v;
                    array_push($dataArr, $pu);
                }
                $tmp['data'] = $dataArr;
//                $item->put('order',$item->groupBy('order_num') );
                //$item->order = $item->groupBy('order_num');
                array_push($arr, $tmp);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $arr;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return null;
        }
    }

    //修改素材信息
    public function updatEmaterial(array $params)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            DB::beginTransaction();
//            if (count($params) > 0) {
//                PtMaterial::where([
//                    'goods_id' => $params[0]['goods_id'],
//                    'supp_id' => $params[0]['supp_id']
//                ])->delete();
//            }
            foreach ($params as $item) {
                PtMaterial::create($item);
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            DB::commit();
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            DB::rollBack();
        }
    }


    //查询所有期数的信息列表
    public function readAllNperDataBiz($supp_id, $nper_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $query = PtPreSale::query();
            if (!empty($nper_id)) {//如果是根据id查询
                $query = $query->where('id', $nper_id)->orderBy('end_date', 'desc')->paginate(Consts::PAGE_SIZE);
            } else {//查询全部
                $goods_ids = PtGoodsSpec::select(DB::raw('distinct(goods_id) as goods_id'))->pluck('goods_id')->all();
                $pt_pre_sale_ids = PtPreSale::whereIn('goods_id', $goods_ids)->pluck('id')->all();
                $query = $query->whereIn('id', $pt_pre_sale_ids)->orderBy('end_date', 'desc')->paginate(Consts::PAGE_SIZE);
            }
            if (count($query->items()) == 0) {
                return null;
            }
            foreach ($query->items() as $pt_pre_sale) {
//                $goods = PtGoodsOrderMid::query()
//                    ->select(DB::raw('SUM(num) as num'),'pt_goods_order_mid.goods_id','pt_goods_order_mid.goods_name',
//                        'pt_goods_order_mid.img','pt_goods_spec.spec_id','pt_goods_spec.name as spec_name')
//                    ->where(['supp_id'=>$supp_id,'pt_pre_sale_id'=>$pt_pre_sale->id])
//                    ->where('status','>=',2)
//                    ->leftJoin('pt_goods_spec','pt_goods_spec.id','=','pt_goods_order_mid.spec_id')
//                    ->groupBy('pt_goods_order_mid.spec_id')
//                    ->get();
//                $pt_pre_sale->goods = $goods;
                if (($pt_pre_sale->end_date) < Carbon::now()) {//活动结束
                    $pt_pre_sale->isend = 1;
                } else {
                    $pt_pre_sale->isend = 0;
                }
                $pt_pre_sale->status = ShopTorageInOut::where(['supp_id' => $supp_id, 'pre_sale_id' => $nper_id])->first()->status;
                $pt_pre_sale->nper = '期数：' . date('Y-m-d H:i', strtotime($pt_pre_sale->start_date)) . '-' . date('Y-m-d H:i', strtotime($pt_pre_sale->end_date));
            }

            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $query;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return null;
        }
    }

    //按期数查询所有订单列表
    public function readOrderMsgByNperIdBiz($supp_id, $nper_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            Log::info($supp_id . '=====' . $nper_id);
            $pre_sale = PtPreSale::where(['id' => $nper_id])->first();
            if ($pre_sale == null) {
                return [];
            }
            PtGoodsOrderMid::select(DB::raw('SUM(num) as num'), 'order_num')->where(['supp_id' => $supp_id, 'pt_pre_sale_id' => $nper_id])->groupBy('order_num');
            $orders = PtGoodsOrderMid::query()
                ->select(DB::raw('SUM(num) as total_order_num'), 'pt_goods_order_mid.goods_id', 'pt_goods_order_mid.goods_name', 'pt_order.addr as location',
                    'pt_goods_order_mid.goods_img as img', 'pt_goods_spec.id as spec_id', 'pt_goods_spec.name as spec_name', 'pt_order.pro', 'pt_order.city', 'pt_order.area')
                ->where(['pt_goods_order_mid.supp_id' => $supp_id, 'pt_goods_order_mid.pt_pre_sale_id' => $nper_id])
                ->where('pt_goods_order_mid.status', '>=', 2)
                ->where('pt_goods_spec.is_online', 0)
                ->leftJoin('pt_goods_spec', 'pt_goods_spec.id', '=', 'pt_goods_order_mid.spec_id')
                ->leftJoin('pt_order', 'pt_order.order_num', '=', 'pt_goods_order_mid.order_num')
                ->groupBy('pt_goods_order_mid.spec_id')
                ->get();
//            Log::info()
//            $orders = PtGoodsOrderMid::query()->select('pt_goods_spec.name as goods_spec','pt_order.addr as location','pt_goods_order_mid.goods_name','pt_goods_order_mid.price','pt_goods_order_mid.num')
//                ->where(['pt_goods_order_mid.pt_pre_sale_id'=>$nper_id,'pt_goods_order_mid.goods_id'=>$pre_sale->goods_id,'pt_goods_order_mid.status'=>2,'pt_goods_order_mid.supp_id'=>$supp_id])
//                ->leftJoin('pt_order','pt_order.order_num','=','pt_goods_order_mid.order_num')
//                ->leftJoin('pt_goods_spec','pt_order.spec_id','=','pt_goods_spec.id')
//                ->get();
            if (count($orders) > 0) {
                $result = $orders->groupBy('location')->toArray();
            } else {
                return [];
            }
            $data = [];
            $arr = [];
            foreach ($result as $k1 => $val) {
                $data['location'] = $k1;
                $data['data'] = $val;
                $arr[] = $data;
            }
            Log::info($arr);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $arr;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return array();
        }
    }


    //发货接口
    public function sendOrderGoodsBiz($supp_id, $purchase_id, array $pickup_ids)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            DB::beginTransaction();
            $now = Carbon::now();
            //查询采购总表获取serial_num
            $serial_num = SuppPurchase::where([
                'id' => $purchase_id
            ])->first()->serial_num;
            foreach ($pickup_ids as $pickup_id){
                //更新出库状态
                $where = ['serial_num' => $serial_num, 'supp_id' => $supp_id, 'pickup_id' => $pickup_id];
                $update = ['status' => 0, 'year' => $now->year, 'month' => $now->month, 'day' => $now->day, 'out_date' => $now];
                $in_out = ShopTorageInOut::query()->updateOrCreate($where,$update);
                //更新订单状态
                PtGoodsOrderMid::where(['supp_id'=>$supp_id,'status'=>2,'serial_num'=>$serial_num,'pickup_id'=>$pickup_id])->update(['status'=>4]);
                //更新订单表的出库入库id
                PtOrder::where(['account_types'=>1,'creator_ownto_id'=>$supp_id,'status'=>2,'serial_num'=>$serial_num,'pickup_id'=>$pickup_id])->update(['torage_inout_id'=>$in_out->id]);

            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            DB::commit();
            if(count($pickup_ids) > 0){
                try {
                    $client = new Client();
                    $response = $client->request('POST', Consts::SYS_SHOP_URL . '/shop/push_template_msg', [
                        'form_params' => [
                            'supp_id' => $supp_id,
                            'purchase_id' => $purchase_id,
                            'pickup_ids' => $pickup_ids
                        ]
                    ]);
                } catch (ErrorException $err_ex) {
                    Log::info('推送失败');
                    Log::info($err_ex);
                } catch (Exception $ex) {
                    Log::info('推送失败');
                }
            }
            return 2;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            DB::rollBack();
            return -4;
        }
    }

    //废弃接口
    public function sendOrderGoodsBiz_nouser($supp_id, $nper_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            DB::beginTransaction();
            //期数表同一发货，状态修改
            PtPreSale::where('unique_id', $nper_id)->update(['status' => 1]);
            //期数表的id有关供应商的商品状态改为配送中
            $order_ids = PtGoodsOrderMid::select(DB::raw('distinct(order_id) as order_id'))->where(['pt_pre_sale_id' => $nper_id, 'status' => 2, 'supp_id' => $supp_id])->pluck('order_id')->all();
            PtGoodsOrderMid::where(['pt_pre_sale_id' => $nper_id, 'status' => 2, 'supp_id' => $supp_id])->update(['status' => 4]);
            //获取所有提货点id
            $pickup_ids = PtOrder::whereIn('id', $order_ids)->pluck('pickup_id')->all();
            foreach ($pickup_ids as $pickup_id) {
                //往出库入库表写入出库数据
                $in_out = ShopTorageInOut::create([
                    'out_type' => 0,
                    'out_date' => Carbon::now(),
                    'supp_id' => $supp_id,
                    'pickup_id' => $pickup_id
                ]);
                $datas = PtGoodsOrderMid::select(DB::raw('SUM(num) as num'), 'goods_id', 'spec_id')->where('pickup_id', $pickup_id)->groupBy('spec_id')->get();
                foreach ($datas as $item) {
                    ShopGoodsInoutMid::create([
                        'inout_id' => $in_out->id,
                        'goods_id' => $item->goods_id,
                        'spec_id' => $item->spec_id,
                        'num' => $item->num
                    ]);
                }
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            DB::commit();
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            DB::rollBack();
        }
    }


    //读取所有的期数
    public function readAllNperBiz($supp_id)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            $pre_sales = PtPreSale::where('supp_id', $supp_id)->get(['id', 'start_date', 'end_date', 'unique_id']);
            foreach ($pre_sales as $item) {
                $item->nper = $item->start_date . '-' . $item->end_date;
            }
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return $pre_sales;
        } catch (PDOException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
        }
    }


}